<?php

namespace Viettrans\FrontBundle\Controller;

use Viettrans\FrontBundle\Entity\Announcement;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class PartialController extends Controller
{
    /**
     *  @Template()
     */
    public function searchAction(Request $request) {
        $requestQuery = $request->query;

        $params = array(
            'keyword'    => $requestQuery->get('keyword'),
            'categoryId' => $requestQuery->get('categoryId'),
            'locality'   => $requestQuery->get('locality'),
            'categories' => $this->get('my_cache_manager')->getCategories(),
            'keyword'    => $requestQuery->get('keyword'),
            'orderBy'    => $requestQuery->get('orderBy'),
            'period'     => $requestQuery->get('period'),
            'numOf'      => $requestQuery->get('numOf'),
        );
        
        return array(
            'params' => $params,
			'route'  => $request->attributes->get('_route'),
        );
    }
    
    /**
     * @Template()
     */
    public function paginationAction(Request $request) {
        # Count announcement
        $em = $this->getDoctrine()->getEntityManager();
        $announcementRepo = $em->getRepository('ViettransFrontBundle:Announcement');
                
        $countAnnouncements = $announcementRepo->countAnnouncements(); 

        $params = $announcementRepo->getParams();
                
        # Get the paginator service
        $paginator = $this->get('my_paginator')
                          ->build($countAnnouncements, $params['currentPage'], $params['maxItemsPerPage'], $params['maxPagesPerSegment']);
        
		$query = $_SERVER['QUERY_STRING'];
		
        return array(
            'paginator' => $paginator,
            'route'     => $request->attributes->get('_route'),
			'query'     => $query,
        );
    }
    
    /**
     * @Template()
     */
    public function topMenuAction(Request $request) {
        $route = $request->attributes->get('_route');

		$em = $this->getDoctrine()->getEntityManager();
        
		$userId = $this->get('session')->get('userId');
		$user = $em->getRepository('ViettransFrontBundle:User')->find($userId);
		
        switch($route) {
            case 'announcement_offer':
                $currentTab = 'announcement_offer';
                break;
            
			case 'announcement_demand':
                $currentTab = 'announcement_demand';
                break;
            
			case 'announcement_new':
            case 'announcement_preview':    
                $currentTab = 'announcement_new';
                break;
            
			case 'announcement_show':
				$id = $request->get('id');

				$announcement = $em->getRepository('ViettransFrontBundle:Announcement')->find($id);
				
				if (!$announcement)	throw $this->createNotFoundException('Không tìm thấy tin đăng tương ứng.');

				$currentTab = ($announcement->getTypeId() == 1) ? 'announcement_offer' : 'announcement_demand';
				break;
			
			case 'servicepro_index':	
                $currentTab = 'servicepro_index';
                break;
				
			default:
                $currentTab = '';
                break;   
        }

        return array(
            'currentTab' => $currentTab,
			'user'       => $user,
        );
    }
    
    /**
     * @Template()
     */
    public function breadScrumbAction(Request $request, $announcement = '') {
		$t = $this->get('translator');
	
		$router = $this->get('router');
        
		$route = $request->attributes->get('_route');

		# Get bread scrum service
		$breadScrumb = $this->get('my_bread_crumb_generator');
				
		# First level
		$breadScrumb->addItem('Viettrans', $router->generate('announcement_offer'));
		
		switch($route) {
			case 'announcement_offer':
				# Second level
				$breadScrumb->addItem($t->trans('layout.header.topMenu.offer'));
				break;
				
			case 'announcement_demand':
				# Second level
				$breadScrumb->addItem($t->trans('layout.header.topMenu.demand'));
				break;
				
			case 'announcement_show':
				# Second level
				$breadScrumb->addItem('Tin '.$announcement->getTypeObj()->getName(), $router->generate('announcement_offer'));
				
				# Third level
				$breadScrumb->addItem($announcement->getTitle()); 
				break;
				
			case 'announcement_new':
			case 'announcement_preview':
			case 'announcement_create':
				# Second level
				$breadScrumb->addItem($t->trans('layout.header.topMenu.create'));
				break;
				
			case 'user_new':
			case 'user_create':
				# Second level
				$breadScrumb->addItem('Tạo tài khoản');
				break;
				
			case 'user_forgot_password':
				# Second level
				$breadScrumb->addItem('Quên mật khẩu');
				break;
				
			case 'user_login':
				# Second level
				$breadScrumb->addItem('Đăng nhập');
				break;
				
			default:
				break;
		}
                                       
        return array(
            'breadscrumb' => $breadScrumb->generate(),
        );
    }
}
